home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / sfit108.zip / DLL / LORENTZ5.C < prev    next >
C/C++ Source or Header  |  1994-11-22  |  5KB  |  160 lines

  1. #define INCL_WIN
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <os2.h>
  7. #include <float.h>
  8. // the standard number of data columns
  9. #define STDCOL 5
  10. #define XSLOT 0
  11. #define YSLOT 1
  12. #define CALCSLOT -3
  13. #define BESTSLOT -2
  14. #define FLAGSLOT -1
  15.  
  16. /* static data and parameters */
  17. static double *data;                    /* start of complete data array  */
  18.  
  19. /* The data is all in a contigous block starting with datx then daty followed by
  20.  * zero or more additional data columns as read in from the data file. The last
  21.  * three slots are: datt , the current best results and the flag array. 
  22.  * Each point not in an exclusion zone has its corresponding flag TRUE. 
  23.  */
  24.  
  25.  
  26. static double *datx;                    /* x data read in from data file */
  27. static double *daty;                    /* y data read in from data file */
  28. static double *datt;                    /* generated data */
  29. static int Ndat;                        /* number of data points */
  30. static char firsttime;
  31. static double **Ptr;                    /* pointer to parameter pointer array */
  32.  
  33. struct info_line
  34.    {
  35.    char *line;
  36.    };
  37.  
  38. /*****************************************************************************/
  39. /* Do not modify any code not enclosed by this and the end banner            */
  40. /*****************************************************************************/
  41.  
  42. /* the next two values are compared to values passed by the program as check */
  43. /* NADD is the number of additional data columns required for this function  */
  44. /* NPARAM is the number of parameters required by this function              */
  45.  
  46. #define NADD 0              
  47. #define NPARAM 16
  48.  
  49. /* edit the following lines or add and delete new lines */ 
  50. static struct info_line dll_info[] = { 
  51.    "This function calculates five lorentzians.",
  52.    "It requires 16 parameters as follows:",
  53.    "P1 is the constant background",
  54.    "P2 is the center posiion of lorentzian 1 along the x axis.",
  55.    "P3 is the amplitude of lorentzian 1.",
  56.    "P4 is the width of lorentzian 1.", 
  57.    "P5 is the center posiion of lorentzian 2 along the x axis.",
  58.    "P6 is the amplitude of lorentzian 2.",
  59.    "P7 is the width of lorentzian 2.", 
  60.    "P8 is the center posiion of lorentzian 3 along the x axis.",
  61.    "P9 is the amplitude of lorentzian 3.",
  62.    "P10is the width of lorentzian 3.", 
  63.    "P11  is the center posiion of lorentzian 4 along the x axis.",
  64.    "P12 is the amplitude of lorentzian 4.",
  65.    "P13 is the width of lorentzian 4.", 
  66.    "P14 is the center posiion of lorentzian 5 along the x axis.",
  67.    "P15 is the amplitude of lorentzian 5.",
  68.    "P16 is the width of lorentzian 5.", 
  69.    NULL};
  70.    
  71. VOID EXPENTRY fitfunc(void)
  72.    {
  73.    int i;
  74.    double wid1, wid2;
  75.    double wid3, wid4, wid5;
  76.    double amp1, amp2;
  77.    double amp3, amp4, amp5;
  78.    double pos1, pos2;
  79.    double pos3, pos4, pos5;
  80.    double E1, E2;
  81.    double E3, E4, E5;
  82.    double lor1, lor2;
  83.    double lor3, lor4, lor5;
  84.    double offset;
  85.  
  86.    /* The next section is executed the first time through only */
  87.    /* so put any initialization code in here                   */
  88.    if (firsttime) 
  89.       {
  90.       firsttime = FALSE;
  91.       /* Mask off all floating point exceptions */
  92.       _control87(MCW_EM,MCW_EM);
  93.       }
  94.  
  95.    /* give parameters more meaningful names  */
  96.    offset=*Ptr[1];
  97.    pos1 = *Ptr[2];
  98.    amp1 = *Ptr[3];
  99.    wid1 = *Ptr[4];
  100.    pos2 = *Ptr[5];
  101.    amp2 = *Ptr[6];
  102.    wid2 = *Ptr[7];
  103.    pos3 = *Ptr[8];
  104.    amp3 = *Ptr[9];
  105.    wid3 = *Ptr[10];
  106.    pos4 = *Ptr[11];
  107.    amp4 = *Ptr[12];
  108.    wid4 = *Ptr[13];
  109.    pos5 = *Ptr[14];
  110.    amp5 = *Ptr[15];
  111.    wid5 = *Ptr[16];
  112.  
  113.    /* This is the main loop that calculates the function datt[] */
  114.    /* for all data points. Put your function in the loop body   */ 
  115.    for (i = 0;i < Ndat;i++)
  116.       {
  117.       E1 = datx[i] - pos1;
  118.       E2 = datx[i] - pos2;
  119.       E3 = datx[i] - pos3;
  120.       E4 = datx[i] - pos4;
  121.       E5 = datx[i] - pos5;
  122.       lor1 = amp1 / (1 + E1*E1/wid1/wid1) ;   /*Lorentzian */
  123.       lor2 = amp2 / (1 + E2*E2/wid2/wid2) ;  
  124.       lor3 = amp3 / (1 + E3*E3/wid3/wid3) ;  
  125.       lor4 = amp4 / (1 + E4*E4/wid4/wid4) ;  
  126.       lor5 = amp5 / (1 + E5*E5/wid5/wid5) ;  
  127.       datt[i] = lor1 + lor2 + lor3 + lor4 + lor5 + offset;
  128.       }
  129.    }
  130.  
  131. /*****************************************************************************/
  132. /* Do not modify any code not enclosed by this and the start banner          */
  133. /*****************************************************************************/
  134.  
  135.  
  136.  
  137. char EXPENTRY init_dll_globals(int N,int mb,int NP,double *dat,double *p[])
  138.    {
  139.    /* set globals */
  140.    firsttime = TRUE;
  141.    Ndat = N;
  142.    if (NADD != mb - STDCOL) return -1;
  143.    if (NPARAM != NP) return -2;
  144.    /* set array pointers */
  145.    data = dat;
  146.    daty = dat + N;
  147.    datx = dat;
  148.    datt = dat + (mb + CALCSLOT) * N;     /* the calculated data goes into the third last slot */
  149.    /* set parameter pointers */
  150.    Ptr = p;
  151.    return 0;
  152.    }
  153.  
  154. struct info_line * EXPENTRY get_dll_info(void)
  155.    {
  156.    /* return pointer to array of info strings set up above */
  157.    return dll_info; 
  158.    }
  159.  
  160.